package com.shop.api;

import com.shop.config.annotation.TokenToUser;
import com.shop.entity.Store;
import com.shop.entity.User;
import com.shop.service.StoreService;
import com.shop.util.Result;
import com.shop.util.ResultGenerator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/store")
public class StoreApi {

    @Resource
    private StoreService storeService;

    @PostMapping("/create")
    public Result createStore(@RequestParam String storeName,@TokenToUser User loginUser) {
        if(loginUser==null){
            return ResultGenerator.genFailResult(50003);//token失效
        }

        int result=storeService.createStore(storeName,loginUser.getUserId());
        if(result==20000){
            log.info("storeApi,创建了一个{}店铺", storeName);
            return ResultGenerator.genSuccessResult();
        }

        return ResultGenerator.genFailResult(result);
    }

    @PostMapping("/findAllStore")
    public Result<List<Store>> findAllStore(@TokenToUser User loginUser) {
        if(loginUser==null){
            return ResultGenerator.genFailResult(50003);//token失效
        }

        List<Store> result=storeService.findAllStore(loginUser.getUserId());
        if(result.size()!=0){
            return ResultGenerator.genSuccessResult(result);
        }

        return ResultGenerator.genFailResult(50010);//没有查到店铺
    }


}
